package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.PageParm;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private AliOSSUtils aliOSSUtils;


    /**
     * 批量增加检查套餐
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result insert(@RequestParam("checkgroupIds") String checkgroupIds,
                         @RequestBody Setmeal setmeal){

        log.info("新增检查套餐:{}",checkgroupIds);
        String[] idList=checkgroupIds.split(",");
        // 基本表插入基本信息

        // 关联表
        for (String id:idList){
            setmeal.setId(Integer.parseInt(id));
            setmealService.add(setmeal);

        }
        return Result.success();
    }

    /**
     * 批量增加检查套餐
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestParam("checkGroupIds") String checkgroupIds,
                         @RequestBody Setmeal setmeal){

        log.info("修改检查套餐:{}",checkgroupIds);
        String[] ids = checkgroupIds.split(",");
        List<String> idList = new ArrayList<String>(Arrays.asList(ids));
        // 修改基本数据
        setmealService.edit(setmeal);
        // 修改关联表
        setmealService.editCheckGroups(idList,setmeal.getId());
        return Result.success();
    }

    /**
     * 查询所有检查套餐
     * @param setmeal
     * @return
     */
    @GetMapping("/findAll")
    public Result findall(Setmeal setmeal){
        log.info("查询所有套餐");
        List<Setmeal> list=setmealService.findall(setmeal);
        return Result.success(list);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findbyid(@RequestParam("id") Integer id){
        log.info("根据id查询套餐");
        Setmeal setmeal=setmealService.findById(id);
        return Result.success(setmeal);
    }

    /**
     * 分页查询
     * @param pageParm
     * @return
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody PageParm pageParm) {
        log.info("分页查询:{},{}",pageParm.getCurrentPage(),pageParm.getPageSize());
        PageResult page = setmealService.page(pageParm.getCurrentPage(),pageParm.getPageSize());
        return page;
    }


    /**
     * 图片上传
     * @param imgFile
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile)throws IOException {
        log.info("图片上传：{}",imgFile.getOriginalFilename());

        String url=aliOSSUtils.upload(imgFile);
        log.info("图片上传完成,文件访问的url:{}",url);
        return Result.success();
    }
}
